/**
 * @license
 * Copyright Alibaba.com All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
 */

import {
  ChangeDetectionStrategy,
  ChangeDetectorRef,
  Component,
  ElementRef,
  EventEmitter,
  Host,
  Input,
  OnDestroy,
  OnInit,
  Output,
  Renderer2,
  TemplateRef,
  ViewEncapsulation
} from '@angular/core';
import {ApesConfigService, collapseMotion, InputBoolean, WithConfig} from "@shared/apes/core";
import {ApesCollapseComponent} from "@shared/apes/collapse/apes-collapse.component";


@Component({
  selector: 'apes-collapse-panel',
  exportAs: 'apesCollapsePanel',
  templateUrl: './apes-collapse-panel.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
  encapsulation: ViewEncapsulation.None,
  animations: [collapseMotion],
  styles: [
    `
      apes-collapse-panel {
        display: block;
      }
    `
  ],
  host: {
    '[class.apes-collapse-no-arrow]': '!apesShowArrow',
    '[class.apes-collapse-item-active]': 'apesActive',
    '[class.apes-collapse-item-disabled]': 'apesDisabled'
  }
})
export class ApesCollapsePanelComponent implements OnInit, OnDestroy {
  prefixCls='apes-collapse';
  @Input() @InputBoolean() apesActive = false;
  @Input() @InputBoolean() apesDisabled = false;
  @Input() @WithConfig(true) @InputBoolean() apesShowArrow: boolean;
  @Input() apesExtra: string | TemplateRef<void>;
  @Input() apesHeader: string | TemplateRef<void>;
  @Input() apesExpandedIcon: string | TemplateRef<void>;
  @Output() readonly apesActiveChange = new EventEmitter<boolean>();

  clickHeader(): void {
    if (!this.apesDisabled) {
      this.apesCollapseComponent.click(this);
    }
  }

  markForCheck(): void {
    this.cdr.markForCheck();
  }

  constructor(
    public apesConfigService: ApesConfigService,
    private cdr: ChangeDetectorRef,
    @Host() private apesCollapseComponent: ApesCollapseComponent,
    elementRef: ElementRef,
    renderer: Renderer2
  ) {
    renderer.addClass(elementRef.nativeElement, 'apes-collapse-item');
  }

  ngOnInit(): void {
    this.apesCollapseComponent.addPanel(this);
  }

  ngOnDestroy(): void {
    this.apesCollapseComponent.removePanel(this);
  }
}
